package com.admin.system.modules.mnt.rest;

import com.admin.common.utils.PageResult;
import com.admin.component.web.MultipartFileUtil;
import com.admin.logging.annotation.Log;
import com.admin.logging.enums.OperateTypeEnum;
import com.admin.system.modules.mnt.dto.ServerQueryDTO;
import com.admin.system.modules.mnt.entity.Server;
import com.admin.system.modules.mnt.service.ServerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author sq-lan
 */
@RestController
@Api(tags = "运维：服务器管理")
@RequiredArgsConstructor
@RequestMapping("/api/serverDeploy")
public class ServerController {

    private final ServerService serverService;

    @ApiOperation(value = "查询服务器")
    @GetMapping
    @PreAuthorize("@ss.check('serverDeploy:list')")
    public ResponseEntity<PageResult<Server>> paging(ServerQueryDTO dto) {
        return new ResponseEntity<>(serverService.paging(dto), HttpStatus.OK);
    }

    @Log(module = "服务器管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation(value = "新增服务器")
    @PostMapping
    @PreAuthorize("@ss.check('serverDeploy:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody Server entity) {
        serverService.save(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log(module = "服务器管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation(value = "修改服务器")
    @PutMapping
    @PreAuthorize("@ss.check('serverDeploy:edit')")
    public ResponseEntity<Object> modify(@Validated @RequestBody Server entity) {
        serverService.updateById(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(module = "服务器管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation(value = "删除Server")
    @DeleteMapping
    @PreAuthorize("@ss.check('serverDeploy:del')")
    public ResponseEntity<Object> remove(@RequestBody Set<Integer> ids) {
        serverService.remove(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log(module = "服务器管理-测试连接")
    @ApiOperation(value = "测试连接服务器")
    @PostMapping("/testConnect")
    @PreAuthorize("@ss.check('serverDeploy:add')")
    public ResponseEntity<Object> testConnect(@Validated @RequestBody Server entity) {
        return new ResponseEntity<>(serverService.testConnect(entity), HttpStatus.CREATED);
    }

    @ApiOperation("导出服务器数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('serverDeploy:list')")
    public void export(HttpServletResponse response, ServerQueryDTO dto) throws IOException {
        List<Server> servers = serverService.list(dto);
        List<Map<String, Object>> list = new ArrayList<>();
        for (Server deploy : servers) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("服务器名称", deploy.getName());
            map.put("服务器IP", deploy.getIp());
            map.put("端口", deploy.getPort());
            map.put("账号", deploy.getAccount());
            map.put("创建日期", deploy.getCreateTime());
            list.add(map);
        }
        MultipartFileUtil.downloadExcel(list, response);
    }
}
